package com.lims.algorithm.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * <功能简述> 加载properties资源文件
 *
 * @author lims
 * @date 2020-03-11 10:08
 */
public class PropertiesUtils {
    /**
     * 日志
     */
    private static final Logger logger = LoggerFactory.getLogger(PropertiesUtils.class);

    /**
     * 单例实例
     */
    private static volatile PropertiesUtils instance;

    private Properties properties = new Properties();

    /**
     * 私有化构造方法
     */
    private PropertiesUtils() {
    }

    /**
     * 加载properties
     *
     * @param path
     */
    private PropertiesUtils(String path) {
        try {
            InputStream is = PropertiesUtils.class.getClassLoader().getResourceAsStream(path);
            properties.load(is);
            if (is != null) {
                is.close();
            }
        } catch (IOException e) {
            logger.error("IOException::load properties error", e);
        }
    }

    /**
     * 定义共有静态方法
     *
     * @param path
     * @return
     */
    public static PropertiesUtils getInstance(String path) {
        if (instance == null) {
            synchronized (PropertiesUtils.class) {
                if (instance == null) {
                    instance = new PropertiesUtils(path);
                }
            }
        }
        return instance;
    }

    /**
     * 获取Properties的值
     *
     * @param key
     * @return
     */
    public String getConfig(String key) {
        return properties.getProperty(key, "");
    }
}









